﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Text.RegularExpressions;

namespace SupplyChainManagement
{
    /// <summary>
    /// This form is the form for adding new inventory
    /// </summary>
    public partial class InventoryAddForm : Form
    {
        private InventoryForm form;

        public InventoryAddForm(InventoryForm form)
        {
            this.form = form;
            InitializeComponent();
        }

        /// <summary>
        /// Close the Add Entry form and reactivate controls for the main form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //check if any of the fields are empty
            if (string.IsNullOrEmpty(txtItemName.Text) || string.IsNullOrEmpty(txtAmount.Text) || string.IsNullOrEmpty(txtMax.Text)
            || string.IsNullOrEmpty(txtMin.Text))
                    MessageBox.Show("Data is not complete");
            //check if price only contains numbers
            else if (Regex.IsMatch(txtAmount.Text, @"^[0-9,-\.]+$") && Regex.IsMatch(txtMin.Text, @"^[0-9,-\.]+$")
                && Regex.IsMatch(txtMax.Text, @"^[0-9,-\.]+$"))
            {
                AddEntry();
                MessageBox.Show("Success!");
                //move back to main form
                this.Close();
                form.Enabled = true;
                form.Activate();
            }
            else MessageBox.Show("Data is not valid.");            
            
        }

        /// <summary>
        /// Add new entry to the database
        /// </summary>
        private void AddEntry()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                //write the entry to inventory
                OdbcCommand sqlCommand = dbMySQL.CreateCommand();                
                sqlCommand.CommandText = "insert into inventory value ('" + txtID.Text + "','" + txtItemName.Text + "','" + txtMax.Text +
                    "','" + txtMin.Text + "')";
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Dispose();

                //get the last index of the rawitem item_id
                //to auto-increment the primary key
                OdbcCommand rawItemCmd = dbMySQL.CreateCommand();
                rawItemCmd.CommandText = "select * from rawitem";
                OdbcDataReader rawReader = rawItemCmd.ExecuteReader();
                int index = 0;
                string last_id = null;
                while (rawReader.Read())
                {
                    last_id = rawReader.GetString(0);
                }
                index = int.Parse(last_id) + 1;
                rawReader.Dispose();

                //inserts new rawitem entry to the table, according to the amount
                int amount = int.Parse(txtAmount.Text);
                for (int i = 0; i < amount; i++)
                {
                    rawItemCmd.CommandText = "insert into rawitem value ('" + index + "','" + txtID.Text + "','" +
                        txtItemName.Text + "','ready')";
                    rawItemCmd.ExecuteNonQuery();
                    index++;
                }
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        //return to the previous form
        private void AddFormClosed(object sender, FormClosedEventArgs e)
        {
            form.Enabled = true;
            form.Activate();
            form.LoadDatabase();
        }

        //return to the previous form
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            form.Enabled = true;
            form.Activate();
        }
    }
}
